<?php 

var_dump(date('Y-m-d'));
   $mtime = microtime(); 
   $mtime = explode(" ",$mtime); 
   $mtime = $mtime[1] + $mtime[0]; 
   $starttime = $mtime;

   

require_once 'CorePhpValidation.php'; 

class abc extends CorePhpValidation {
	
	protected function getDefaultMefssage($rule_name) {
		self::$default_messages['required'] = 'This filed is required wow';
		self::$default_messages['number'] = 'Should be number';
		self::$default_messages['pattern'] = 'Pattern does not match';
		self::$default_messages['fileIniSize'] = 'File is too large. Maximum allowed ' . ini_get('upload_max_filesize') . ' ~ Error Code: ' . UPLOAD_ERR_INI_SIZE;
		self::$default_messages['fileFormSize'] = 'File is too large ~ Error Code: ' . UPLOAD_ERR_FORM_SIZE;
		self::$default_messages['filePartial'] = 'File partially uploaded ~ Error Code: ' . UPLOAD_ERR_PARTIAL;
		self::$default_messages['fileNoTmpDir'] = 'Upload failed ~ Error Code: ' . UPLOAD_ERR_NO_TMP_DIR;
		self::$default_messages['fileCantWrite'] = "Upload failed. Can't write file on disk ~ Error Code: " . UPLOAD_ERR_CANT_WRITE;
		self::$default_messages['filePhpExtension'] = 'Upload failed ~ Error Code: ' . UPLOAD_ERR_EXTENSION;

		return isset(self::$default_messages[$rule_name]) ? self::$default_messages[$rule_name] : 'This field has errors [ Exact error message not assigned ] ';
	}
	
	protected function uniqueEmails($value, $arg1, $arg2) {
	//	var_dump($value);
	//	var_dump($arg1);
		//var_dump($arg2);
		
		//$this->getDefaultMessage($rule_name)
		
		var_dump('inside this custom');
		
		return false;
	}
	
	public static function uniqueEmail ($value, $arg1, $arg2) {
		var_dump('inside static custom');
	}
	
}

class cde {
	public function custom_uniqueEmail ($value, $arg1, $arg2) {
		var_dump('inside outside class object custom');
	}
}

$obj = new cde();

function uniqueEmail($value, $arg1, $arg2) {
		var_dump('inside inline custom');
		//var_dump($arg1);
		//var_dump($arg2);
		//var_dump($value);
		return false;
}


$rules['test'] = array('rules'=>array('alphaNumeric'));

$rules['custom'] = array('rules'=> array('custom_uniqueEmail'=>array('call_type'=>'inline', 'call_options'=>array('class_name'=>'abc'), 'parameters'=>array('0'=>'arg1', '1'=>'arg2'))),
												 'messages'=>array('required'=>'its required', 'custom_uniqueEmail'=>'Email Id not unique'));		

$rules['lname'] = array('rules'=> array('number', 'pattern'=>array("regex=>"=>"/^[0-9]+$/")),
								 		'messages'=>array( 
										'number'=>'Should be a number',
										'pattern'=>'Only number allowed'));

$rules['multi'] = array('rules'=> array('exactLength'=>array('length'=>'5')),
										'messages'=>array('exactLength'=>'Should be of length 5')
);

$rules['yes_no'] = array('rules'=> array('required'));

$rules['fruits'] = array('rules'=> array('checkboxResquired', 'checkboxMinLength'=>array('2')), 'messages'=>array('checkboxMinLength'=>'Min 2 plz'));

$rules['slt'] = array('rules'=> array(
'multiselectMinLength'=>array('length'=>'2', 'empty_option_value'=>''), 
'multiselectMaxLength'=>array('length'=>'3', 'empty_option_value'=>'')),
'messages'=>array('multiselectMinLength'=>'Min 2', 'multiselectMaxLength'=>'Max 3'));

$rules['fname'] = array('rules'=> array(
'required', 
'pattern'=>array("regex=>"=>"/^[a-zA-Z]+$/")),
								 		'messages'=>array('required'=>'This field is required', 
										'pattern'=>'Only alphabets allowed'));


$rules['upload'] = array('rules'=> array(
												'fileRequired',
												'fileExtensions'=>array('valid_extensions'=>array('jpg', 'gif')), 
												'fileMimeTypes'=> array('valid_mime_types'=>array('image/jpeg')),
												//'fileUploadSuccess',
												'fileMaxSize' => array('size'=>'1000024')),

								 		'messages'=>array(
										'fileRequired'=>'This field is required',
										'fileExtensions'=>'Invalid extension',
										'fileMimeTypes'=>'Invalid mime type',
										'fileMaxSize'=>'Exceed max size'));


$rules['pwd'] = array('rules'=>array('matchFields'=>array('pwd1')),
											'messages'=>array('matchFields'=>'Pwds do not match !!'));
//echo "<pre>";
//var_dump($rules['upload']);


$obj = abc::initializeValidation();
$obj->setValidationRules($rules);
//$obj->addRule('test_filed')->ruleName('required');
//$obj->addRule('test_filed')->ruleName('pattern')->regex('sdsd');

//$obj->

if(isset($_POST['submit'])) {
	
	$array = array();
	$obj->setValidationData($_POST, $_FILES);
	
	if($obj->valid()) {
		echo 'passs';
	} else {
		echo 'fail';
	}
	
}

//echo '<pre>';
//print_r($rules);

var_dump($_POST);

?>

<html>
<body>
<form method="post" enctype="multipart/form-data">

<br />
Test: <textarea name="test"></textarea>
<?php echo $obj->getError('test'); ?>

<br />
Pwd: <input type="text" name="pwd" value="" />
<?php echo $obj->getError('pwd'); ?>

<br />
Repwd: <input type="text" name="pwd1" value="" />
<?php echo $obj->getError('pwd'); ?>

<br />
<input type="text" name="fname" value="" />
<?php echo $obj->getError('fname'); ?>
<br />
<input type="text" name="lname" value="" />
<?php echo $obj->getError('lname'); ?>

<br />
<textarea name="multi"></textarea>
<?php echo $obj->getError('multi'); ?>

<br />
<input type="file" name="upload" />
<?php echo $obj->getError('upload'); ?>

<br/>
<input type="text" name="custom" value="" />
<?php echo $obj->getError('custom'); ?>

<br/>
<input type="radio" name="yes_no" value="yes" />Yes
<input type="radio" name="yes_no" value="no" />No
<input type="radio" name="yes_no" value="may_be" />May be
<?php echo $obj->getError('yes_no'); ?>

<br/>
<input type="checkbox" name="fruits[]" value="apple" />Apple
<input type="checkbox" name="fruits[]" value="orange" />Orange
<input type="checkbox" name="fruits[]" value="grapes" />Grapes
<?php echo $obj->getError('fruits'); ?>


<br/>
<select name="slt[]" multiple="multiple">
<option value="">select</option>
<optgroup label="Good">
<option value="yes">yes</option>
<option value="no">no</option>
</optgroup>
<optgroup label="Poor">
<option value="maybe">may be</option>
<option value="maybe1">may be1</option>
<option value="maybe2">may be2</option>
</optgroup>
</select>
<?php echo $obj->getError('slt'); ?>


<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>

</html>

<?php 
$mtime = microtime(); 
   $mtime = explode(" ",$mtime); 
   $mtime = $mtime[1] + $mtime[0]; 
   $endtime = $mtime; 
   $totaltime = ($endtime - $starttime); 
   echo "This page was created in ".$totaltime." seconds"; 
   
   ?>

